package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.MallShopListResponse
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ItemShopsBinding
import com.gexiaobao.pigeon.ui.view.StarRatingBar
import com.makeramen.roundedimageview.RoundedImageView

/**
 * @Author      : hxw
 * @Date        : 2024/3/26 9:46
 * @Describe    :
 */
class MallShopAdapter(data: ArrayList<MallShopListResponse.ListBean>) :
    BaseQuickAdapter<MallShopListResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_shops, data) {

    private var mOnClick: OnItemShopClickListener? = null
    private var mOnClickShopGoods: OnItemShopGoodsClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: MallShopListResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemShopsBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val itemStarShop = holder.getView<StarRatingBar>(R.id.itemStarShop)
        itemStarShop.setStar(item.score.toFloat())
        val ivItemShopHead = holder.getView<RoundedImageView>(R.id.ivItemShopHead)
        ImageLoaderManager.loadCircleImageFall(context, item.shopPicUrl, ivItemShopHead, R.mipmap.icon_default_head_shop)

        holder.itemView.apply {
            binding.rvItemShop.adapter = null
            if (item.isEmpty() || item.goodList.size == 0) {
                holder.setGone(R.id.rvItemShop, true)
                return
            }
            holder.setGone(R.id.rvItemShop, false)
            val mAdapterGoods = ShopGoodsItemAdapter(item.goodList)
            binding.rvItemShop.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                binding.rvItemShop.addItemDecoration(SpaceItemDecoration(Util.dp2px(context, 3f), 0))
                adapter = mAdapterGoods
                mAdapterGoods.notifyDataSetChanged()

            }
            mAdapterGoods.addChildClickViewIds(R.id.itemShopGoodsImage)
            mAdapterGoods.setOnItemChildClickListener { _, _, position ->
                if (mOnClickShopGoods != null) {
                    mOnClickShopGoods!!.onItemClickShopGoodsListener(holder.layoutPosition, position)
                }
            }
        }

        holder.itemView.setOnClickListener {
            if (mOnClick != null) {
                mOnClick!!.onItemClick(holder.layoutPosition)
            }
        }
    }

    interface OnItemShopClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemShopClickListener(onItemClickListener: OnItemShopClickListener) {
        mOnClick = onItemClickListener
    }

    interface OnItemShopGoodsClickListener {
        fun onItemClickShopGoodsListener(positionShop: Int, positionGoods: Int)
    }

    fun setOnItemShopGoodsClickListener(onItemClickListener: OnItemShopGoodsClickListener) {
        mOnClickShopGoods = onItemClickListener
    }
}